package com.se2.antiqueexhibition.Service.impl;

import com.se2.antiqueexhibition.Mapper.CollectionMapper;
import com.se2.antiqueexhibition.Service.CollectionService;
import com.se2.antiqueexhibition.pojo.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public boolean addCollection(Integer userId, Integer relicId) {
        // 检查是否已收藏
        if (isCollected(userId, relicId)) {
            return false;
        }
        Collection collection = new Collection();
        collection.setUserId(userId);
        collection.setRelicId(relicId);
        return collectionMapper.insert(collection) > 0;
    }

    @Override
    public Collection getCollectionById(Integer id) {
        return collectionMapper.selectById(id);
    }

    @Override
    public List<Collection> getCollectionsByUserId(Integer userId) {
        return collectionMapper.selectByUserId(userId);
    }

    @Override
    public List<Collection> getCollectionsByRelicId(Integer relicId) {
        return collectionMapper.selectByRelicId(relicId);
    }

    @Override
    public List<Collection> getAllCollections() {
        return collectionMapper.selectAll();
    }

    @Override
    public boolean cancelCollection(Integer userId, Integer relicId) {
        return collectionMapper.deleteByUserIdAndRelicId(userId, relicId) > 0;
    }

    @Override
    public boolean deleteCollection(Integer id) {
        return collectionMapper.deleteById(id) > 0;
    }

    @Override
    public boolean isCollected(Integer userId, Integer relicId) {
        return collectionMapper.countByUserIdAndRelicId(userId, relicId) > 0;
    }
}
